﻿using G.Service.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace G.Service.BLL
{
    public static class AppBLL
    {
        /// <summary>
        /// 添加APP
        /// </summary>
        /// <param name="appName"></param>
        /// <param name="desciption"></param>
        /// <returns></returns>
        public static Result<G.Service.Data.Application> AddApplication(string appName, string desciption)
        {
            if (string.IsNullOrEmpty(appName))
                return new Result<G.Service.Data.Application>() { Msg = "appName不能为空！" };
            Data.APPData app = new Data.APPData();
            G.Service.Data.Application application = new Data.Application() { APPName = appName, CreateTime = DateTime.Now, Description = desciption };
            application.AppKey = Guid.NewGuid().ToString();
            app.AddApplication(application);
            app.SaveChanges();
            return new Result<G.Service.Data.Application>() { IsOK = true, ResponseData = application };
        }

        public static Result AddAppEvent(string eventName, string desciption)
        {
            if (string.IsNullOrEmpty(eventName))
                return new Result() { Msg = "eventName不能为空！" };
            Data.APPData app = new Data.APPData();
            app.AddAppEvent(new Data.APPEvent() { EventName = eventName, Description = desciption });
            app.SaveChanges();
            return new Result() { IsOK = true };
        }
        /// <summary>
        /// 添加客户端事件
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="eventid"></param>
        /// <param name="eventContent"></param>
        /// <returns></returns>
        public static Result AddEvent(string appkey, int eventid, string eventContent)
        {
            if (string.IsNullOrEmpty(appkey))
                return new Result() { Msg = "appkey不能为空！" };

            Data.APPData app = new Data.APPData();
            Application application = app.GetApplicationByAppKey(appkey);
            if (application == null)
                return new Result() { Msg = "无效的appkey" };

            APPEvent e = app.GetAPPEventById(eventid);
            if (e == null)
                return new Result() { Msg = "无效的eventid" };


            app.AddEvent(new Event() { AppId = application.ID, EventID = e.Id, Date = DateTime.Now, EventContent = eventContent});
            app.SaveChanges();
            return new Result() { IsOK = true };
        }
    }
}
